package cn.xfdzx.android.apps.shop.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;

import cn.xfdzx.android.apps.shop.R;

/**
 * Creator: Gao MinMin.
 * Date: 2018/10/25.
 * Description: 图片加载工具类.
 */
public class ImageGlideUtil {

    private ImageGlideUtil() {
        throw new UnsupportedOperationException("Can't instantiate me...");
    }

    /**
     * 加载本地图片.
     */
    public static void loadLocalImage(Context context, ImageView imageView, int imageRes) {
        Glide.with(context).load(imageRes).apply(new RequestOptions().centerCrop()).into(imageView);
    }

    /**
     * 加载图片.
     */
    public static void loadImage(Context context, ImageView imageView, String imageUrl, int placeholderResId) {
        RequestBuilder<Drawable> requestBuilder = Glide.with(context)
                .load(imageUrl);
        RequestOptions requestOptions = new RequestOptions().centerCrop().placeholder(R.drawable.icon_defult).format(DecodeFormat.PREFER_RGB_565);

        if(placeholderResId>0) {
            requestBuilder.thumbnail(Glide.with(context).load(placeholderResId));
        }
        requestBuilder
                .load(imageUrl)
//                .transition(DrawableTransitionOptions.withCrossFade())
                .apply(requestOptions)
                .into(imageView);
    }

    //以图片宽度为基准
    public static void showImageWidthRatio(Context context,String url, final ImageView imageView, final int width){
        RequestOptions requestOptions = new RequestOptions().centerCrop().format(DecodeFormat.PREFER_RGB_565);
        Glide.with(context)
                .asBitmap()
                .apply(requestOptions)
                .load(url)
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        int imageWidth=resource.getWidth();
                        int imageHeight=resource.getHeight();
                        int height = width * imageHeight / imageWidth;
                        ViewGroup.LayoutParams params=imageView.getLayoutParams();
                        params.height=height;
                        params.width=width;
                        imageView.setImageBitmap(resource);
                    }
                });
    }

    /**
     * 加载图片.
     */
    public static void loadImage(Context context, ImageView imageView, String imageUrl) {
        loadImage(context, imageView, imageUrl, -1);
    }

    /**
     * 加载视频页图片.
     */
    public static void loadVideoImage(Context context, ImageView imageView, String imageUrl) {
        Glide.with(context)
                .load(imageUrl)
                .transition(DrawableTransitionOptions.withCrossFade())
                .apply(new RequestOptions().centerCrop())
                .thumbnail(Glide.with(context).load(imageUrl).override(60))
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                .into(imageView);
    }

    /**
     * 加载图片.
     */
    public static void loadImage(Context context, ImageView imageView, int imageRes) {
        Glide.with(context)
                .load(imageRes)
                .transition(DrawableTransitionOptions.withCrossFade())
                .apply(new RequestOptions().centerCrop())
                .into(imageView);
    }

    /**
     * 加载头像.
     */
    public static void loadAvatar(Context context, ImageView imageView, String imageUrl) {
        Glide.with(context)
                .load(imageUrl)
                .apply(new RequestOptions().centerCrop().placeholder(R.drawable.icon_my_head))
                .into(imageView);
    }

    /**
     * 加载首页上3图片.
     */
    public static void loadFitsrImg(Context context, ImageView imageView, String imageUrl) {
        Glide.with(context)
                .load(imageUrl)
                .apply(new RequestOptions())
                .into(imageView);
    }

    /**
     * 加载头像.
     */
    public static void loadGoodsType(Context context, ImageView imageView, String imageUrl) {
        Glide.with(context)
                .load(imageUrl)
                .apply(new RequestOptions().fitCenter())
                .into(imageView);
    }

    /**
     * 根据图片地址获取Bitmap.
     */
    public static void getBitmapFromUrl(Context context, String imageUrl, final GetBitmapCallback getBitmapCallback) {
        Glide.with(context).asBitmap()
                .load(imageUrl)
                .apply(new RequestOptions().centerCrop())
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        getBitmapCallback.asBitmap(resource);
                    }
                });
    }

    /**
     * 加载图片.
     */
    private static void load(Context context, ImageView imageView, String imageUrl,
                             int thumbnailResId, int placeholderResId, int errorResId, boolean isCircle) {
        RequestBuilder<Drawable> requestBuilder = Glide.with(context)
                .load(imageUrl);
        RequestOptions requestOptions = new RequestOptions();

        if (thumbnailResId != -1)
            requestBuilder.thumbnail(Glide.with(context).load(thumbnailResId));

        if (placeholderResId != -1)
            requestOptions.placeholder(placeholderResId);

        if (errorResId != -1)
            requestOptions.error(errorResId);

        if (isCircle)
            requestOptions.circleCrop().autoClone();
        else
            requestOptions.centerCrop();

        requestBuilder
                .transition(DrawableTransitionOptions.withCrossFade())
                .apply(requestOptions)
                .load(imageUrl)
                .into(imageView);
    }

    public interface GetBitmapCallback {

        void asBitmap(Bitmap bitmap);
    }
}
